package lolic;

import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

/**
 * 
 * @author Dave DePiano
 *
 */
public class LblItemSlotTransferHandler extends TransferHandler
{
	
	DataFlavor flavor;
	String strData;
	List list;
	
	public LblItemSlotTransferHandler(List list) {
		this.list = list;
	  }
	
	public boolean canImport(JComponent dest, DataFlavor[] flavors) {
	    // i feel lucky
		try {
			flavor = flavors[0];	
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
	    return true;
	  }

	
	 public boolean importData(JComponent src, Transferable transferable) 
	 {		    
		    try {
		    	strData = (String)transferable.getTransferData(flavor);
			} catch (UnsupportedFlavorException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}		    
		    
		    if(src.getClass().equals(JLabel.class))
		    {
		    	JLabel lblSource = (JLabel)src;
		    	//lblSource.setText(strData);
		    	Item item = Item.getMatchingItem(strData, list);
		    	lblSource.setIcon(item.getImgPortrait());
		    	lblSource.setToolTipText(strData);
		    	GUI.btnSave.setBackground(Color.RED);
		    	GUI.packFrame();
		    }
		    
		    return true;
	 }
}
